package com.fzt.system.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzt.framework.user.UserDetail;
import com.fzt.framework.utils.TreeUtils;
import com.fzt.system.convert.SysMenuConvert;
import com.fzt.system.domain.entity.SysMenuEntity;
import com.fzt.system.domain.vo.SysMenuVO;
import com.fzt.system.mapper.SysMenuMapper;
import com.fzt.system.mapper.SysRoleMenuMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author 非洲铜
 * @since 2022-12-08
 */
@AllArgsConstructor
@Service
public class SysMenuService extends ServiceImpl<SysMenuMapper, SysMenuEntity> {
    private final SysMenuMapper     sysMenuMapper;

    /**
     * 获取用户的权限标识
     */
    public Set<String> getUserAuthority(UserDetail userDetail) {
        if (userDetail.getSuperAdmin() == 1) {
            return sysMenuMapper.getAuthorities();
        } else {
            return new HashSet<>(sysMenuMapper.getUserAuthorities(userDetail.getId()));
        }
    }

    /**
     * 获取用户菜单
     *
     * @param type 菜单类型
     */
    public List<SysMenuVO> getUserMenuTree(UserDetail user, int type) {
        List<SysMenuEntity> menuList;
        if (user.getSuperAdmin() == 1) {
            menuList = sysMenuMapper.getMenuList(type);
        } else {
            menuList = sysMenuMapper.getUserMenuList(type);
        }
        return TreeUtils.build(SysMenuConvert.INSTANCE.convertList(menuList));
    }
}
